﻿using System.Linq;
using HK.Editor.Common;
using HK.Editor.Utils;
using UnityEditor;
using UnityEngine;
using HLogger = HK.Core.Logs.Logger;

namespace HK.Editor.GUIEx.Styles
{
	/// <summary>
	/// GUI风格
	/// </summary>
	public static class GuiExHelper 
	{

		public const string PAGE_INFO_FORMAT = "{0} / {1}";

#region Styles

		internal static GUIStyle PaginationText;
		internal static GUIStyle PageSizeTextField;
		internal static GUIStyle DraggingHandle;
		internal static GUIStyle HeaderBackground;
		internal static GUIStyle BoldHeaderBackground;
		internal static GUIStyle FooterBackground;
		internal static GUIStyle PaginationHeader;
		internal static GUIStyle BoxBackground;
		internal static GUIStyle PreButton;

		private static GUIStyle _dragAreaBackground = null;
		internal static GUIStyle DragAreaBackground
		{
			get
			{
				if (null != _dragAreaBackground)
				{
					if (null != _dragAreaBackground.normal.background)
					{
						return _dragAreaBackground;
					}
				}

#if DLL_ICONS_LOAD
				var noticeBgTex = UtilsDlls.LoadIcon(EditorConst.BorderNotice, 16, 16);
#else
				var noticeBgTex = AssetDatabase.LoadAssetAtPath<Texture2D>(EditorConst.BorderNotice);
#endif
				_dragAreaBackground = new GUIStyle
				{
					normal =
					{
						background = noticeBgTex
					},
					onNormal =
					{
						background = noticeBgTex
					},
					onFocused = 
					{
						background = noticeBgTex
					},
					border = new RectOffset(2, 2, 2, 2)
				}; 
				return _dragAreaBackground;
			}
		}
		
		private static GUIStyle _clickableBackground = null;
		internal static GUIStyle ClickableBackground
		{
			get
			{
				if (null != _clickableBackground)
				{
					if (null != _clickableBackground.normal.background)
					{
						return _clickableBackground;
					}
				}


#if DLL_ICONS_LOAD
				var activatedBgTex = UtilsDlls.LoadIcon(EditorConst.BorderNotice, 16, 16);
#else
				var activatedBgTex = AssetDatabase.LoadAssetAtPath<Texture2D>(EditorConst.BorderNotice);
#endif
				_clickableBackground = new GUIStyle
				{
					normal =
					{
						background = activatedBgTex
					},
					onNormal =
					{
						background = activatedBgTex
					},
					onFocused = 
					{
						background = activatedBgTex
					},
					border = new RectOffset(2, 2, 2, 2)
				}; 
				return _clickableBackground;
			}
		}
		
		/// <summary>
		/// 无文字只有Icon的按钮
		/// </summary>
		internal static GUIStyle IconButton;
		internal static GUIStyle PreButtonStretch;
		internal static GUIStyle ElementBackground;
		internal static GUIStyle VerticalLabel;
		internal static GUIStyle MiddleLeftLabel;
		internal static GUIStyle MiddleCenterLabel;
		internal static GUIStyle MiddleRightLabel;
		internal static GUIStyle MiddleCenterText;
		internal static GUIStyle MiddleLeftText;
		internal static GUIStyle GroupBox;
		internal static GUIStyle TitleLeft;
		internal static GUIStyle FoldoutHeader;
        
		/// <summary>
		/// 克隆GUIStyle
		/// </summary>
		/// <param name="iCustomStyleName">欲克隆的自定义风格名</param>
		/// <param name="iFixedWidth">填充宽度</param>
		/// <param name="iFixedHeight">填充高度</param>
		/// <returns></returns>
		public static GUIStyle CloneStyle(string iCustomStyleName, float iFixedWidth = -1, float iFixedHeight = -1)
		{
			var tempList = GUI.skin.customStyles
				.Where(iO => iCustomStyleName.Equals(iO.name))
				.ToArray();
			if (0 >= tempList.Length)
			{
				HLogger.Error($"GuiExHelper::CloneStyle():Clone Failed!!({iCustomStyleName} is not exist in GUI.skin.customStyles!)");
				return null;
			}
			var customStyle = new GUIStyle(tempList[0]);
			if (0 < iFixedWidth)
			{
				customStyle.fixedWidth = iFixedWidth;
			}
			if (0 < iFixedHeight)
			{
				customStyle.fixedHeight = iFixedHeight;
			}
			return customStyle;
		}
		
		/// <summary>
		/// Style初始化
		/// </summary>
		private static void InitStyles()
		{
			PaginationText = new GUIStyle
			{
				margin = new RectOffset(2, 2, 0, 0),
				fontSize = EditorStyles.miniTextField.fontSize,
				font = EditorStyles.miniFont,
				normal =
				{
					textColor = EditorStyles.miniTextField.normal.textColor
				},
				alignment = TextAnchor.MiddleLeft,
				clipping = TextClipping.Clip
			};

#if UNITY_2019_3_OR_NEWER
			PageSizeTextField = new GUIStyle("RL Background");
#else
			pageSizeTextField = new GUIStyle("RL Footer");
			pageSizeTextField.overflow = new RectOffset(0, 0, -2, -3);
			pageSizeTextField.contentOffset = new Vector2(0, -1);
#endif
			PageSizeTextField.alignment = TextAnchor.MiddleLeft;
			PageSizeTextField.clipping = TextClipping.Clip;
			PageSizeTextField.fixedHeight = 0;
			PageSizeTextField.padding = new RectOffset(3, 0, 0, 0);
			PageSizeTextField.font = EditorStyles.miniFont;
			PageSizeTextField.fontSize = EditorStyles.miniTextField.fontSize;
			PageSizeTextField.fontStyle = FontStyle.Normal;
			PageSizeTextField.wordWrap = false;

			DraggingHandle = new GUIStyle("RL DragHandle");
			HeaderBackground = new GUIStyle("RL Header")
			{
				fixedHeight = 0,
				stretchHeight = false
			};
			BoldHeaderBackground= new GUIStyle("RL Header")
			{
				fixedHeight = 38
			};
			FooterBackground = new GUIStyle("RL Footer");
			
#if UNITY_2019_3_OR_NEWER
			PaginationHeader = new GUIStyle("TimeRulerBackground")
			{
				fixedHeight = 0
			};
#else
			paginationHeader = new GUIStyle("RL Element");
			paginationHeader.border = new RectOffset(2, 3, 2, 3);
#endif
			ElementBackground = new GUIStyle("RL Element")
			{
				border = new RectOffset(2, 3, 2, 3)
			};
			VerticalLabel = new GUIStyle(EditorStyles.label)
			{
				alignment = TextAnchor.UpperLeft,
				contentOffset = new Vector2(10, 3)
			};
			MiddleLeftLabel =  new GUIStyle(EditorStyles.label)
			{
				alignment = TextAnchor.MiddleLeft,
				contentOffset = new Vector2(2, 10),
				padding = new RectOffset(5,5, -7, 12)
			};
			MiddleCenterLabel =  new GUIStyle(EditorStyles.label)
			{
				alignment = TextAnchor.MiddleCenter,
				contentOffset = new Vector2(2, 10),
				padding = new RectOffset(5,5, -7, 12)
			};
			MiddleLeftText = new GUIStyle("MiniTextField")
			{
				alignment = TextAnchor.MiddleLeft,
				contentOffset = new Vector2(2, 10),
				padding = new RectOffset(5,5, -7, 12)
			};
			MiddleCenterText = new GUIStyle("MiniTextField")
			{
				alignment = TextAnchor.MiddleCenter,
				contentOffset = new Vector2(2, 10),
				padding = new RectOffset(5,5, -7, 12)
			};
			
			MiddleRightLabel =  new GUIStyle(EditorStyles.label)
			{
				alignment = TextAnchor.MiddleRight,
				contentOffset = new Vector2(10, 10),
				padding = new RectOffset(0,15, -7, 12)
			};
			BoxBackground = new GUIStyle("RL Background")
			{
				border = new RectOffset(6, 3, 3, 6)
			};

#if UNITY_2019_3_OR_NEWER
			PreButton = new GUIStyle("RL FooterButton");
#else
			preButton = new GUIStyle("RL FooterButton");
			preButton.contentOffset = new Vector2(0, -4);
#endif
			IconButton = new GUIStyle("LODRendererAddButton");
			var contentOffset = IconButton.contentOffset;
			contentOffset.y += 2.0f;
			IconButton.contentOffset = contentOffset;

			PreButtonStretch = new GUIStyle("RL FooterButton")
			{
				fixedHeight = 0,
				stretchHeight = true
			};
			
			GroupBox = new GUIStyle("GroupBox")
			{
				alignment = TextAnchor.MiddleLeft,
				fixedHeight = 0,
				stretchHeight = true
			};
			GroupBox.padding.left += 2;
			GroupBox.padding.right += 2;

			TitleLeft = new GUIStyle("IN TitleText")
			{
				alignment = TextAnchor.MiddleLeft,
				fontStyle = FontStyle.Bold,
				fixedHeight = 0,
				stretchHeight = true
			};
			TitleLeft.padding.left += 2;
			TitleLeft.padding.right += 2;
			
			FoldoutHeader = new GUIStyle("Foldout")
			{
				clipping = TextClipping.Clip
			};
		}
        
#endregion

#region Contents


		internal static GUIContent AddIconBtn;
		internal static GUIContent AddMoreIconBtn;
		internal static GUIContent DelIconBtn;

		/// <summary>
		/// 窗口最大化按钮
		/// <para>* 视图右上角，窗体最大化按钮</para>
		/// <para>* 实际用途为，展开列表中所有Element</para>
		/// </summary>
		internal static GUIContent MaxWinBtn;
				
		/// <summary>
		/// 窗口最小化按钮
		/// <para>* 视图右上角，窗体最小化按钮</para>
		/// <para>* 实际用途为，让列表中所有Element不展开</para>
		/// </summary>
		internal static GUIContent MinWinBtn;
        
		internal static GUIContent SortAscending;
		internal static GUIContent SortDescending;

		/// <summary>
		/// 平台/渠道 - iOS
		/// </summary>
		internal static GUIContent IosContent = null;
		
		/// <summary>
		/// 平台/渠道 - Android
		/// </summary>
		internal static GUIContent AndroidContent = null;
		
		/// <summary>
		/// 平台/渠道 - Ps4
		/// </summary>
		internal static GUIContent Ps4Content = null;
		
		/// <summary>
		/// 平台/渠道 - Ps5
		/// </summary>
		internal static GUIContent Ps5Content = null;
		
		/// <summary>
		/// 平台/渠道 - XboxOne
		/// </summary>
		internal static GUIContent XboxOneContent = null;
		
		/// <summary>
		/// 平台/渠道 - Pc
		/// </summary>
		internal static GUIContent PcOneContent = null;
		
		/// <summary>
		/// 按钮 - 追加
		/// </summary>
		internal static GUIContent AddBtnContent = null;
		
		/// <summary>
		/// 按钮 - 保存
		/// </summary>
		internal static GUIContent SaveBtnContent = null;
		
		/// <summary>
		/// 按钮 - Apply
		/// </summary>
		internal static GUIContent ApplyBtnContent = null;
		
		/// <summary>
		/// 按钮 - 浏览
		/// </summary>
		internal static GUIContent BrowseBtnContent = null;
		
		/// <summary>
		/// 按钮 - 编辑
		/// <para>* Icon Only</para>
		/// </summary>
		internal static GUIContent EditIconBtnContent = null;

		/// <summary>
		/// 按钮 - 帮助
		/// <para>* Icon Only</para>
		/// </summary>
		internal static GUIContent HelpIconBtnContent = null;
        
		/// <summary>
		/// 按钮 - 删除
		/// <para>* Icon Only</para>
		/// </summary>
		internal static GUIContent DelIconBtnContent = null;
		
		/// <summary>
		/// 分页按钮 : 上一页
		/// </summary>
		internal static GUIContent PaginationPrevBtn;
		
		/// <summary>
		/// 分页按钮 : 下一页
		/// </summary>
		internal static GUIContent PaginationNextBtn;
		
		/// <summary>
		/// 分页下来列表
		/// </summary>
		internal static GUIContent PaginationPopup;

		/// <summary>
		/// 列表图标
		/// </summary>
		internal static GUIContent ListIcon;
		
		/// <summary>
		/// 初始化Content
		/// </summary>
		private static void InitContents()
		{
			AddIconBtn = EditorGUIUtility.IconContent("Toolbar Plus", "Add to list");
			AddMoreIconBtn = EditorGUIUtility.IconContent("Toolbar Plus More", "Choose to add to list");
			DelIconBtn = EditorGUIUtility.IconContent("Toolbar Minus", "Remove selection from list");
			PaginationPrevBtn = EditorGUIUtility.IconContent("Animation.PrevKey", "上一页");
			PaginationPrevBtn.tooltip = "上一页";
			
			PaginationNextBtn = EditorGUIUtility.IconContent("Animation.NextKey", "下一页");
			PaginationNextBtn.tooltip = "下一页";

#if UNITY_2018_3_OR_NEWER
			PaginationPopup = EditorGUIUtility.IconContent("PopupCurveEditorDropDown", "请选择分页!");
#else
			PaginationPopup = EditorGUIUtility.IconContent("MiniPopupNoBg", "请选择分页!");
#endif
			PaginationPopup.tooltip = "请选择分页!";

			ListIcon = EditorGUIUtility.IconContent("align_horizontally_right");
			
			MaxWinBtn = EditorGUIUtility.IconContent("winbtn_win_max");
			MaxWinBtn.tooltip = "展开所有子Element";
			
			MinWinBtn = EditorGUIUtility.IconContent("winbtn_win_min");
			MinWinBtn.tooltip = "让所有子Element不展开";
			
			SortAscending = EditorGUIUtility.IconContent("align_vertically_bottom");
			SortAscending.tooltip = "升序";

			SortDescending = EditorGUIUtility.IconContent("align_vertically_top");
			SortDescending.tooltip = "降序";
			
			// 平台/渠道
			IosContent = UtilsGUIEx.CreateGuiContentWithIcon("iOS", EditorConst.IconIOS);
			AndroidContent = UtilsGUIEx.CreateGuiContentWithIcon("Android", EditorConst.IconAndroid);
			Ps4Content = UtilsGUIEx.CreateGuiContentWithIcon("Ps4", EditorConst.IconPs4);
			Ps5Content = UtilsGUIEx.CreateGuiContentWithIcon("Ps5", EditorConst.IconPs5);
			XboxOneContent = UtilsGUIEx.CreateGuiContentWithIcon("XboxOne", EditorConst.IconXbox);
			PcOneContent = UtilsGUIEx.CreateGuiContentWithIcon("Pc", EditorConst.IconPc);
			
			// 按钮
			AddBtnContent = UtilsGUIEx.CreateGuiContentWithIcon("追加", EditorConst.IconAdd1);
			SaveBtnContent = UtilsGUIEx.CreateGuiContentWithIcon("保存", EditorConst.IconSave);
			ApplyBtnContent = UtilsGUIEx.CreateGuiContentWithIcon("应用", EditorConst.IconApply);
			BrowseBtnContent = UtilsGUIEx.CreateGuiContentWithIcon("浏览", EditorConst.IconBrowse);
			
			// 按钮 - Icon Only
			EditIconBtnContent = UtilsGUIEx.CreateGuiContentWithIcon("", EditorConst.IconEdit);
			DelIconBtnContent = UtilsGUIEx.CreateGuiContentWithIcon("", EditorConst.IconDel);
			HelpIconBtnContent = UtilsGUIEx.CreateGuiContentWithIcon("", UtilsGUIEx.BUILD_IN_ICON_NOTICE_HELP);
			
		}

#endregion
		
		static GuiExHelper() 
		{
			// Style初始化
			InitStyles();
			
			// 初始化Content
			InitContents();
		}
	}
}